/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProfileComponent;

import Bean.Account.*;
import DB.DBConnection;
import java.sql.*;

/**
 *
 * @author DIT-0958372
 */
public class ProfileManagement {

    protected Connection conn;
    protected PreparedStatement pstmt;
    protected ResultSet rs;
    protected AccountSC user = null;

    public boolean updateProfile(AccountSC user, String acCondition) {
        boolean isDone = false;
        String sql = "";
        try {
            //get DB connection
            DBConnection db = new DBConnection();
            conn = db.getConnection();

//            ----------------------This is to change user's basic information-------
            if (acCondition.equals("basicInfo")) {
                sql = "call updateBasicInfo(?,?,?,?,?,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, user.getAcName());
                pstmt.setString(2, user.getUserName());
                pstmt.setString(3, user.getSex());
                pstmt.setString(4, user.getMaritalStatus());
                pstmt.setString(5, user.getBloodType());
                pstmt.setString(6, user.getHometown());
                pstmt.setString(7, user.getCurrentLocation());
                pstmt.setString(8, user.getDOB());

            } else if (acCondition.equals("contactInfo")) {
                sql = "call updateContactInfo(?,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, user.getAcName());
                pstmt.setString(2, user.getHp());
                pstmt.setString(3, user.getEmailAddress());
                pstmt.setString(4, user.getMsn());

            } else if (acCondition.equals("workInfo")) {
                sql = "call updateWorkInfo(?,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, user.getAcName());
                pstmt.setString(2, user.getCompany());
                pstmt.setString(3, user.getDepartment());
                pstmt.setString(4, user.getWorkDay());


            } else if (acCondition.equals("eduInfo")) {
                sql = "call updateEduInfo(?,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, user.getAcName());
                pstmt.setString(2, user.getSchool());
                pstmt.setString(3, user.getSchlClass());
                pstmt.setString(4, user.getEnrollmentDay());
            }


            int check = pstmt.executeUpdate();
            if (check == 1) {
                isDone = true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            this.close();
        }
        return isDone;
    }

    protected void close() {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
